<?php
class Admin_RoleController
    extends Admin_Abstract
{
    public function indexAction()
    {
        $this->_permision('role');
        $this->loadLayout();
        $this->renderLayout();
    }

    public function viewAction()
    {
        $this->_permision('role');
        $this->loadLayout();
        $this->renderLayout();
    }

    public function saveAction()
    {
        $this->_permision('role');
        $data = $this->getRequest()->getParam('role',array());
        $continue = $this->getRequest()->getParam('continue',0);

        try {
            $roleModel = new Model_Role_Admin_Role();
            if(isset($data['id'])) {
                $roleModel->load($data['id']);
                unset($data['id']);
            } else {
                $ruleModel->setCreatedAt(now());
            }

            $roleModel->setPermision(trim($data['permission'],' ,'));
            unset($data['permission']);

            foreach ($data as $key=>$value) {
                $roleModel->setData($key,$value);
            }

            $roleModel->setUpdatedAt(now());
            $roleModel->save();

            $this->_getSession()->addSuccess('Role successfully saved.');

            if($continue) {
                $this->_redirect('admin/role/view/item/'.$roleModel->getId());
            } else {
                $this->_redirect('admin/role/index/');
            }
        } catch (Exception $e) {
            $this->_getSession()->addError($e->getMessage());
            $this->_getSession()->addError('Cannot save role');
            if($continue) {
                if(isset($data['id'])) {
                    $this->_redirect('admin/role/view/');
                } else {
                    $this->_redirect('admin/role/view/item/'.$data['id']);
                }
            } else {
                $this->_redirect('admin/role/index/');
            }
        }
    }

    public function testAction()
    {
        $userModel = new Model_Admin_User_Admin();
        $userModel->load(6);
        $this->_getSession()->setUser($userModel);
        
        $helper = new Admin_Permission_Resource();
        $helper->checkPermision('system');
    }
}